package cn.tedu.csmall.product.web;

import cn.tedu.csmall.product.ex.ServiceException;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

@Data
public class JsonResult<T> implements Serializable {
    @ApiModelProperty("状态代码")
    private Integer state;
    @ApiModelProperty("消息")
    private String stateMessage;
    /**
     * 操作成功时相应的数据
     */
    @ApiModelProperty("数据")
    private T data;

    public static JsonResult ok(){
        return ok(null);
    }
    public static <T> JsonResult ok(T data){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setState(ServiceCode.OK.getValue());
        jsonResult.setData(data);
        return jsonResult;
    }
    public static JsonResult fail(ServiceCode code, String message){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setState(code.getValue());
        jsonResult.setStateMessage(message);
        return jsonResult;
    }
    public static JsonResult fail(ServiceException e){
        return fail(e.getServiceCode(),e.getMessage());
    }
}
